﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FileSystem.Services.IServices;
using FileSystem.Services.Realization;
using FileSystem.ViewModels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace FileSystem.Views {
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class UsersPage : ContentPage {
        public UsersPage() {
            InitializeComponent();
        }

        private async void MenuItem_OnClicked(object sender, EventArgs e) {
            // The sender is the menuItem
            var menuItem = sender as MenuItem;

            // Access the list item through the BindingContext
            var contextItem = menuItem.BindingContext as UserViewModel;

            // Do something with the contextItem here
            var choice = menuItem.Text;
            switch (choice) {
                case "编辑": {
                    var contentPageActivationService =
                        new ContentPageActivationService();
                    var contentNavigationService =
                        new ContentNavigationService(
                            contentPageActivationService);

                    NavigationPage.SetHasNavigationBar(
                        contentPageActivationService.Activate(
                            ContentNavigationConstants.EditUserPage), false);

                    await contentNavigationService.NavigateToAsync(
                        ContentNavigationConstants.EditUserPage, contextItem);

                    break;
                }
                case "添加": {
                    var contentPageActivationService =
                        new ContentPageActivationService();
                    var contentNavigationService =
                        new ContentNavigationService(
                            contentPageActivationService);

                    NavigationPage.SetHasNavigationBar(
                        contentPageActivationService.Activate(
                            ContentNavigationConstants.AddUserPage), false);

                    await contentNavigationService.NavigateToAsync(
                        ContentNavigationConstants.AddUserPage);
                    break;
                }
                case "删除": {
                    var delete =
                        await DisplayAlert("提示", "你想要删除该用户吗?", "是", "否");
                    if (delete)
                        (BindingContext as UsersPageViewModel).Users.Remove(
                            contextItem as UserViewModel);
                    break;
                }
            }
        }
    }
}